import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/custom_select_function.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/get_dict_all_entity.dart';
import 'package:safetymonitor/app/data/entities/application/preventive_rectification_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

///待完成
class NoCompletedController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();
  String searchX = '';


  /// 获取数据
  Future<List<PreventiveRectificationEntity>> getCompanyDeviceListEntity(int index, int size,
      {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<PreventiveRectificationEntity>>(
      Interface.preventiveRectification,
      queryParameters: {
        'pageNo': index,
        'pageSize': size,
        'preventiveName': searchX,
        'status': dictEntity1?.getId(),
      },
    ).execute();
    List<PreventiveRectificationEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
        //删除7（已完成）
        list = list?.where((element) => element.status != 7).toList();

      }
    });
    return list ?? [];
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
   selectCurrencyLevelType();
  }

  ///根据后端返回的状态跳转不同的界面
  void onClickDetail(PreventiveRectificationEntity item) {
    switch (item.status) {
      case 1:
        // '待派发';
        Get.toNamed(Routes.noDistributedStatPage,arguments: item.id);
        break;
      case 2:
        // '待整改';
        Get.toNamed(Routes.noRectificationStatPage,arguments: item.id);
        break;
      case 3:
        // '整改中';
        Get.toNamed(Routes.underRectificationStatPage,arguments: item.id);
        break;
      case 4:
      case 8:
        // '待验收';
        Get.toNamed(Routes.pendingAcceptanceStatPage,arguments: item.id);
        break;
      case 5:
        // '逾期未整改';
        // Get.toNamed(Routes.addHazardPage,arguments: item.id);
        Get.toNamed(Routes.noRectificationStatPage,arguments: item.id);
        break;
      case 6:
        // '逾期未派发';
        // Get.toNamed(Routes.addHazardPage,arguments: item.id);
        Get.toNamed(Routes.noDistributedStatPage,arguments: item.id);
        break;
      case 7:
        // '已完成';
        Get.toNamed(Routes.completedStatePage,arguments: item.id);
        break;
      default:
        AppToast.showError('暂无该状态');
        break;
    }
  }
  static List<GetDictAllEntity> currencyState = [];
  GetDictAllEntity? dictEntity1;

  @override
  void onInit() {
    super.onInit();
    initCurrencyState();
  }

  /// 状态(1:待派发、2:待整改、3:整改中、4:待验收、5:逾期未整改、6:逾期未派发、7:已完成),示例值(1)
  void initCurrencyState() {
    currencyState.clear();
    final GetDictAllEntity getDictAllEntity0 = GetDictAllEntity();
    getDictAllEntity0.id = '';
    getDictAllEntity0.dictionaryLabel = '所有';
    currencyState.add(getDictAllEntity0);
    final GetDictAllEntity getDictAllEntity = GetDictAllEntity();
    getDictAllEntity.id = '1';
    getDictAllEntity.dictionaryLabel = '待派发';
    currencyState.add(getDictAllEntity);
    final GetDictAllEntity getDictAllEntity2 = GetDictAllEntity();
    getDictAllEntity2.id = '2';
    getDictAllEntity2.dictionaryLabel = '待整改';
    currencyState.add(getDictAllEntity2);
    final GetDictAllEntity getDictAllEntity3 = GetDictAllEntity();
    getDictAllEntity3.id = '3';
    getDictAllEntity3.dictionaryLabel = '整改中';
    currencyState.add(getDictAllEntity3);
    final GetDictAllEntity getDictAllEntity4 = GetDictAllEntity();
    getDictAllEntity4.id = '4';
    getDictAllEntity4.dictionaryLabel = '待验收';
    currencyState.add(getDictAllEntity4);
    final GetDictAllEntity getDictAllEntity5 = GetDictAllEntity();
    getDictAllEntity5.id = '5';
    getDictAllEntity5.dictionaryLabel = '逾期未整改';
    currencyState.add(getDictAllEntity5);
    final GetDictAllEntity getDictAllEntity6 = GetDictAllEntity();
    getDictAllEntity6.id = '6';
    getDictAllEntity6.dictionaryLabel = '逾期未派发';
    currencyState.add(getDictAllEntity6);
  }
  ///状态
  void selectCurrencyLevelType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '状态',
      dataList: currencyState,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
        globalKey.currentState?.onRefresh();
      },
    );
  }
}
